package cn.edu.baiyunu.myapplication.ui.screens.home.components

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import cn.edu.baiyunu.myapplication.ui.components.GlassmorphismCard
import java.text.NumberFormat
import java.util.Locale

/**
 * 首页头部卡片，显示用户名称和月度收支汇总信息
 */
@Composable
fun HomeHeader(
    username: String,
    monthlyIncome: Double,
    monthlyExpense: Double,
    monthlyBalance: Double,
    currentYear: Int,
    currentMonth: Int,
    modifier: Modifier = Modifier
) {
    Column(modifier = modifier.fillMaxWidth()) {
        // 欢迎信息
        Text(
            text = "你好，$username",
            style = MaterialTheme.typography.headlineMedium,
            fontWeight = FontWeight.Bold
        )
        
        Spacer(modifier = Modifier.height(8.dp))
        
        // 月份标题
        Text(
            text = "${currentYear}年${currentMonth}月收支",
            style = MaterialTheme.typography.bodyLarge,
            color = MaterialTheme.colorScheme.onSurfaceVariant
        )
        
        Spacer(modifier = Modifier.height(16.dp))
        
        // 毛玻璃效果的收支汇总卡片
        GlassmorphismCard(
            modifier = Modifier.fillMaxWidth()
        ) {
            Column(
                modifier = Modifier.padding(16.dp)
            ) {
                // 当月结余
                Text(
                    text = "当月结余",
                    style = MaterialTheme.typography.bodyLarge,
                    color = MaterialTheme.colorScheme.onSurfaceVariant
                )
                
                Spacer(modifier = Modifier.height(8.dp))
                
                // 结余金额
                val formattedBalance = formatCurrency(monthlyBalance)
                Text(
                    text = formattedBalance,
                    style = MaterialTheme.typography.headlineMedium,
                    fontWeight = FontWeight.Bold,
                    color = when {
                        monthlyBalance > 0 -> MaterialTheme.colorScheme.tertiary
                        monthlyBalance < 0 -> MaterialTheme.colorScheme.error
                        else -> MaterialTheme.colorScheme.onSurface
                    }
                )
                
                Spacer(modifier = Modifier.height(16.dp))
                
                // 收入和支出
                Column {
                    // 收入
                    Text(
                        text = "收入: ${formatCurrency(monthlyIncome)}",
                        style = MaterialTheme.typography.bodyLarge,
                        color = MaterialTheme.colorScheme.tertiary
                    )
                    
                    Spacer(modifier = Modifier.height(4.dp))
                    
                    // 支出
                    Text(
                        text = "支出: ${formatCurrency(monthlyExpense)}",
                        style = MaterialTheme.typography.bodyLarge,
                        color = MaterialTheme.colorScheme.error
                    )
                }
            }
        }
    }
}

/**
 * 格式化金额为货币格式
 */
private fun formatCurrency(amount: Double): String {
    val format = NumberFormat.getCurrencyInstance(Locale.CHINA)
    return format.format(amount).replace("¥", "￥")
} 